// src/api/user.js
import request from "@/utils/request";
import { 
  adminDynamicRoutes, 
  userDynamicRoutes, 
  guestDynamicRoutes 
} from "@/store/mock-dynamic-routes.example";

// 模拟用户数据
const mockUsers = [
  {
    username: "admin",
    password: "123456",
    role: "admin",
    userInfo: {
      id: 1,
      username: "admin",
      nickname: "管理员",
      avatar: "https://avatars.githubusercontent.com/u/1?v=4",
      email: "admin@example.com",
      phone: "13800138000",
      role: "admin",
      dynamicRoutes: adminDynamicRoutes  // 管理员拥有所有权限
    }
  },
  {
    username: "user",
    password: "123456", 
    role: "user",
    userInfo: {
      id: 2,
      username: "user",
      nickname: "普通用户",
      avatar: "https://avatars.githubusercontent.com/u/2?v=4",
      email: "user@example.com",
      phone: "13800138001",
      role: "user",
      dynamicRoutes: userDynamicRoutes  // 普通用户只有部分权限
    }
  },
  {
    username: "guest",
    password: "123456",
    role: "guest", 
    userInfo: {
      id: 3,
      username: "guest",
      nickname: "访客",
      avatar: "https://avatars.githubusercontent.com/u/3?v=4",
      email: "guest@example.com",
      phone: "13800138002",
      role: "guest",
      dynamicRoutes: guestDynamicRoutes  // 访客只有最少权限
    }
  }
];

// 模拟延迟
const delay = (ms = 500) => new Promise(resolve => setTimeout(resolve, ms));

export const userApi = {
  // 模拟登录
  async login(data) {
    await delay(800); // 模拟网络延迟
    
    const { username, password } = data;
    const user = mockUsers.find(u => u.username === username && u.password === password);
    
    if (!user) {
      return Promise.reject(new Error("用户名或密码错误"));
    }
    
    // 生成模拟token
    const token = `mock_token_${user.username}_${Date.now()}`;
    
    return {
      code: 0,
      message: "登录成功",
      data: {
        token,
        userInfo: user.userInfo
      }
    };
  },

  // 模拟获取用户信息
  async getUserInfo() {
    await delay(300);
    
    // 从localStorage获取token，根据token确定用户身份
    const token = localStorage.getItem("token");
    let currentUser = null;
    
    if (token && token.startsWith("mock_token_")) {
      // 从token中提取用户名
      const username = token.split("_")[2];
      const user = mockUsers.find(u => u.username === username);
      if (user) {
        currentUser = user.userInfo;
      }
    }
    
    // 如果没有找到用户，默认返回admin用户信息
    if (!currentUser) {
      currentUser = mockUsers[0].userInfo; // admin用户
    }
    
    return {
      code: 0,
      message: "获取成功",
      data: currentUser
    };
  },

  // 模拟获取用户列表
  async getUserList(params) {
    await delay(500);
    
    const { page = 1, size = 10, keyword = "" } = params;
    
    // 模拟分页数据
    const allUsers = mockUsers.map(user => ({
      id: user.userInfo.id,
      username: user.userInfo.username,
      nickname: user.userInfo.nickname,
      email: user.userInfo.email,
      phone: user.userInfo.phone,
      role: user.role,
      status: 1,
      createTime: "2024-01-01 00:00:00"
    }));
    
    const filteredUsers = keyword 
      ? allUsers.filter(user => 
          user.username.includes(keyword) || 
          user.nickname.includes(keyword) ||
          user.email.includes(keyword)
        )
      : allUsers;
    
    const start = (page - 1) * size;
    const end = start + size;
    const list = filteredUsers.slice(start, end);
    
    return {
      code: 0,
      message: "获取成功",
      data: {
        list,
        total: filteredUsers.length,
        page,
        size
      }
    };
  },

  // 模拟添加用户
  async addUser(data) {
    await delay(500);
    
    return {
      code: 0,
      message: "添加成功",
      data: { id: Date.now() }
    };
  },

  // 模拟更新用户
  async updateUser(data) {
    await delay(500);
    
    return {
      code: 0,
      message: "更新成功",
      data: null
    };
  },

  // 模拟删除用户
  async deleteUser(id) {
    await delay(500);
    
    return {
      code: 0,
      message: "删除成功",
      data: null
    };
  },
};
